package com.xp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xp.entity.RoleMenu;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 角色菜单关联数据访问层
 * 
 * @author xp
 * @since 2024-01-01
 */
@Mapper
public interface RoleMenuMapper extends BaseMapper<RoleMenu> {

    /**
     * 根据角色ID查询菜单ID列表
     * 
     * @param roleId 角色ID
     * @return 菜单ID列表
     */
    @Select("SELECT menu_id FROM sys_role_menu WHERE role_id = #{roleId} AND deleted = 0")
    List<Long> selectMenuIdsByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据菜单ID查询角色ID列表
     * 
     * @param menuId 菜单ID
     * @return 角色ID列表
     */
    @Select("SELECT role_id FROM sys_role_menu WHERE menu_id = #{menuId} AND deleted = 0")
    List<Long> selectRoleIdsByMenuId(@Param("menuId") Long menuId);

    /**
     * 删除角色的所有菜单关联
     * 
     * @param roleId 角色ID
     * @return 删除的记录数
     */
    @Delete("UPDATE sys_role_menu SET deleted = 1 WHERE role_id = #{roleId}")
    int deleteByRoleId(@Param("roleId") Long roleId);

    /**
     * 删除菜单的所有角色关联
     * 
     * @param menuId 菜单ID
     * @return 删除的记录数
     */
    @Delete("UPDATE sys_role_menu SET deleted = 1 WHERE menu_id = #{menuId}")
    int deleteByMenuId(@Param("menuId") Long menuId);
}